<?php

include_once ('model/productModel.php');
include_once ('_include/common.php');

class productController {

	private $ProductID;

	public function __construct() {
		if (!$_REQUEST['pagesize']) {
			$_REQUEST['pagesize'] = 5;
		}
		if (!$_REQUEST['page']) {
			$_REQUEST['page'] = 1;
		}
		$this -> pagesize = $_REQUEST['pagesize'];
		$this -> currentpage = $_REQUEST['page'];
		$this -> model = new productModel($this -> pagesize);
	}

	public function invoke() {
		if ($_REQUEST['Submit']) {

			switch ($_REQUEST['Submit']) {
				case 'Add' :
					$this -> addProduct();
					break;
				case 'Modify' :
					$this -> modifyProduct();
					break;
				case 'Search' :
					$this -> searchProduct();
					break;
			}

		} else {
			if ($_REQUEST['do']) {
				switch ($_REQUEST['do']) {
					case 'select' :
						$this -> loadProduct();
						break;
					case 'delete' :
						$this -> deleteProduct();
						break;
				}
			} else {
				$this -> loadNewProducts();
			}
		}
	}

	public function loadProduct() {
		$ProID = $_REQUEST["ProductID"];
		$model = new productModel(1);
		$product = $model -> selectProductDetail($ProID);
		foreach ($product as $pro) {
			echo json_encode($pro);
		}
	}

	public function loadNewProducts($ProductID = 0) {
		$products = $this -> model -> selectNewProducts($this -> currentpage);
		$pagecount = $this -> model -> pagecount;
		$categories = $this -> model -> selectCategories();

		$PageLink = getPageLinks($pagecount);
		$MSS = '';

		include 'view/admin/product.php';
	}

	public function searchProduct() {
		$filter = "WHERE 1 ";

		if ($_REQUEST["Name"]) {
			$filter .= "&& Name like '" . $_REQUEST["Name"] . "'";
		}

		if ($_REQUEST["PriceFrom"]) {
			$filter .= "&& Price > '" . $_REQUEST["PriceFrom"] . "'";
		}

		if ($_REQUEST["PriceFromTo"]) {
			$filter .= "&& Price < '" . $_REQUEST["PriceTo"] . "'";
		}

		$products = $this -> model -> searchProduct($filter);
		$pagecount = $this -> model -> pagecount;
		$categories = $this -> model -> selectCategories();

		$PageLink = getPageLinks($pagecount);
		$MSS = '';

		include 'view/admin/product.php';
	}

	public function addProduct() {
		set_time_limit(0);
		ini_set('memory_limit', '-1');

		$Name = $_REQUEST['Name'];
		$Category = $_REQUEST['Category'];
		$Price = $_REQUEST['Price'];
		$Description = $_REQUEST['Description'];
		$Quantity = $_REQUEST['Quantity'];
		$Available = $_REQUEST['Available'];
		$FILE = $_FILES['ImageName'];

		$MSS = $this -> model -> insertProduct($Category, $Quantity, $Name, $Description, $Price, $FILE);
		$products = $this -> model -> selectNewProducts();
		$pagecount = $this -> model -> pagecount;
		$categories = $this -> model -> selectCategories();
		$PageLink = getPageLinks($pagecount);

		include 'view/admin/product.php';

	}

	public function modifyProduct() {

		$ProductID = $_REQUEST['ProductID'];
		$Name = $_REQUEST['Name'];
		$Category = $_REQUEST['Category'];
		$Price = $_REQUEST['Price'];
		$Description = $_REQUEST['Description'];
		$Quantity = $_REQUEST['Quantity'];
		$FILE = $_FILES['ImageName'];
		$MSS = $this -> model -> modifyProduct($ProductID, $Category, $Quantity, $Name, $Description, $Price, $FILE);
		$products = $this -> model -> selectNewProducts();
		$pagecount = $this -> model -> pagecount;
		$categories = $this -> model -> selectCategories();
		$PageLink = getPageLinks($pagecount);

		include 'view/admin/product.php';

	}

	public function deleteProduct() {
		$ProductID = $_REQUEST["ProductID"];
		$model = new productModel(1);
		$MSS = $model -> deleteProduct($ProductID);
		echo $MSS;
	}

}
